import 'package:flutter/material.dart';
import 'package:im_module/friend/FriendWdiget.dart';
import 'package:im_module/info/UserInfoWidget.dart';
import 'package:im_module/manager/ImChatManager.dart';
import 'package:im_module/message/ImChatWidget.dart';
import 'package:im_module/provider/im_provider.dart';
import 'package:im_module/search/SearchFriendWidget.dart';
import 'package:im_module/widget/ImHeadImageWidget.dart';

import 'package:provider/provider.dart';
import 'package:tencent_im_sdk_plugin/models/v2_tim_friend_info.dart';

class FriendListWidget extends StatefulWidget {
  final QueryCallback ageCallBack;

  FriendListWidget(this.ageCallBack);

  @override
  _FriendListWidgetState createState() => _FriendListWidgetState();
}

class _FriendListWidgetState extends State<FriendListWidget> {
  @override
  void initState() {
    super.initState();

    //获取好友
    ImChatManager().getFriendList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我的好友"),
      ),
      body: Consumer<ImProvider>(
        builder: (context, _provider, _) {
          if (_provider.friendList.isEmpty) {
            return Center(
              child: Text("暂无好友"),
            );
          } else {
            return ListView.separated(
              itemBuilder: (context, index) {
                //return buildFriend(_provider.friendList[index]);
                //return FriendWdiget(_provider.friendList[index],widget.ageCallBack);

                V2TimFriendInfo application = _provider.friendList[index];

                return ListTile(
                  leading:
                      ImHeadImageWidget("${application.userProfile?.faceUrl}"),
                  title: Text("${application.userProfile?.nickName}"),
                  onTap: ()  async{
                    bool result =  await Navigator.push(context, MaterialPageRoute(builder: (_) {
                      return UserInfoWidget(
                          application.userID, widget.ageCallBack);
                    }));

                    if(result){
                      ImChatManager().getFriendList();
                    }
                  },
                );
              },
              separatorBuilder: (context, index) => Divider(),
              itemCount: _provider.friendList.length,
            );
          }
        },
      ),
    );
  }
}
